<template>
  <div>
    <div>
      <el-row>
        <el-col :span="24">
          <div class="grid-content bg-purple-dark" style="width: 100%">
            <el-form :inline="true" :v-model="list" class="demo-form-inline">
              <el-form-item label="学科名称">
                <el-input v-model="fillterParams.subjectName" placeholder="学科名称"></el-input>
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="fillterParams.subjectName=''">清除</el-button>
                <el-button type="primary" @click="onSubmit">搜索</el-button>
                <el-button type="success" size="small" icon="el-icon-edit" class="right" @click="apPend">新增学科</el-button>
              </el-form-item>
            </el-form>
          </div>
          <el-row>
            <el-col :span="24" type="flex" justify="start">
              <div class="bg-purple-dark el-alert"><i class="el-icon-info"></i>数据一共{{total}}条</div>
            </el-col>
          </el-row>
        </el-col>
      </el-row>
    </div>
    <el-table :data="list" border style="width: 100%,text-align:center">
      <el-table-column type="index" :index="handleIndex" label="序号" width="150"> </el-table-column>
      <el-table-column prop="subjectName" label="学科名称" width="150"> </el-table-column>
      <el-table-column prop="username" label="创建者" width="120"> </el-table-column>
      <el-table-column prop="addDate" label="创建日期" sortable=""> <template v-slot="{ row }">
          {{ row.addDate | formatTime }}
        </template></el-table-column>
      <el-table-column label="前台是否显示" width="120">
        <template v-slot='{row}'>
          {{row.isFrontDisplay? '是' : '否'}}
        </template>
      </el-table-column>
      <el-table-column prop="tags" label="二级目录" width="100"> </el-table-column>
      <el-table-column prop="totals" label="标签" width="100"> </el-table-column>
      <el-table-column prop="twoLevelDirectory" label="题目数量" width="120"> </el-table-column>
      <el-table-column label="操作" width="230">
        <template v-slot="{ row }">
          <el-button type="text" size="small" @click="$router.push('/subjects/directorys')">学科分类</el-button>
          <el-button type="text" size="small" @click="$router.push('/subjects/tags')">学科标签</el-button>
          <el-button type="text" size="small" @click="addList(row)">修改</el-button>
          <el-button type="text" size="small" @click="deleList(row)">删除</el-button>
        </template>
      </el-table-column>

    </el-table>
    <subjects :type='type' :dialogVisible.sync="dialogVisible" :row='row' @Kong='row={}'></subjects>
    <!-- 分页 -->
    <el-row style="margin-top: 20px; text-align: right">
      <el-pagination :page-size="fillterParams.pagesize" :page-sizes="pagesizes" layout="sizes, prev, pager, next, jumper, ->, total" :total="total" @current-change="changePager" @size-change="changeSize" />
    </el-row>

  </div>
</template>

<script>
import { list, remove } from '@/api/hmmm/subjects.js'
import subjects from '../../module-hmmm/components/subjects-add.vue'
export default {
  name: 'subject',
  components: {
    subjects
  },

  data () {
    return {
      // 每页条数选择列表
      pagesizes: [5, 20, 30, 40, 50, 100],
      // 修改新增的
      row: {
        subjectName: '',
        isFrontDisplay: 0
      },
      // 标志位
      type: '',
      // 弹窗
      dialogVisible: false,
      // 查询参数
      fillterParams: {
        page: 1,
        pagesize: 5,
        subjectName: ''
      },
      // 获取角色列表
      list: [],
      // 角色列表的信息总数
      total: 0
    }
  },
  created () {
    this.loadRoleList()
  },

  methods: {
    // 动态控制索引
    handleIndex (index) {
      // 当前页前面的页数 * 每页条数 + index + 1
      const { page, pagesize } = this.fillterParams
      return (page - 1) * pagesize + index + 1
    },
    // 监听每页条数的变化
    changeSize (size) {
      // 更新请求参数中的每页的条数，重新调用接口即可
      this.fillterParams.pagesize = size
      this.loadRoleList()
    },
    // 监听页码的切换
    changePager (page) {
      // 更新接口参数中的页码，重新调用接口即可
      this.fillterParams.page = page
      this.loadRoleList()
    },
    // 搜索列表
    async onSubmit () {
      const str = this.fillterParams.subjectName.trim()
      if (!str) return
      this.fillterParams.subjectName = str
      const ret = await list(this.fillterParams)
      console.log(ret)
      this.list = ret.data.items
      this.total = ret.data.counts
    },
    // 新增学科列表
    apPend () {
      this.dialogVisible = true
      this.type = 'add'
      // this.$refs.form.resetFields()
      this.loadRoleList()
    },
    // 加载列表
    async loadRoleList () {
      try {
        const ret = await list(this.fillterParams)
        // console.log(ret)
        this.list = ret.data.items
        this.total = ret.data.counts
        // this.$message.success('获取加载列表成功')
      } catch {
        this.$message.error('获取学科列表失败')
      }
    },
    // 修改列表
    async addList (row) {
      this.type = 'upd'
      this.dialogVisible = true
      this.row = row
    },
    // 删除学科
    async deleList (row) {
      console.log(row)
      const ret = await this.$confirm('确认要删除吗', '提示', {
        type: 'warning'
      }).catch(err => err)
      // 点击取消
      if (ret === 'cancel') return
      try {
        const dele = await remove(row)
        console.log(dele)
        if (dele.status === 200) {
          this.$message.success('删除成功')
          // 重新调用加载列表接口
          this.loadRoleList()
        } else {
          this.$message.error('删除失败')
        }
      } catch {
        this.$message.error('删除失败')
      }
    }
  }
}
</script>

<style scoped lang="scss">
.right {
  position: absolute;
  right: -600px;
  bottom: -1px;
}
.arTt {
  position: absolute;
  right: -130px;
  bottom: -20px;
}
.border {
  position: absolute;
  top: 25px;
  //  border: 1px solid red;
}
.el-row {
  margin-bottom: 20px;
  &:last-child {
    margin-bottom: 0;
  }
}
.el-col {
  border-radius: 4px;
}
.bg-purple-dark {
  background: #fff;
}
.bg-purple {
  background: #d3dce6;
}
.bg-purple-light {
  background: #e5e9f2;
}
.grid-content {
  margin: 5px;
  border-radius: 4px;
  min-height: 80px;
}
.row-bg {
  padding: 10px 0;
  background-color: #f9fafc;
}
.demo-form-inline {
  position: absolute;
  top: 30px;
  left: 30px;
}
.el-alert {
  width: 100%;
  padding: 8px 16px;
  margin: 0;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  border-radius: 4px;
  position: relative;
  background-color: #f4f4f5;
  color: #909399;
  overflow: hidden;
  opacity: 1;
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-align: center;
  -ms-flex-align: center;
  align-items: center;
  -webkit-transition: opacity 0.2s;
  transition: opacity 0.2s;
  font-size: 13px;
}
</style>
